package com.example.websocket_demo.dao.user;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface UserDao {

    @Insert("<script> insert into user <trim prefix='(' suffix=')' suffixOverrides=','> <if test='username != null and username != \"\"'>username,</if> <if test='passward != null and passward != \"\"'>passward,</if> <if test='phone != null and phone != \"\"'>phone,</if> <if test='email != null and email != \"\"'>email,</if> <if test='nickname != null and nickname != \"\"'>nickname,</if> </trim> <trim prefix='values (' suffix=')' suffixOverrides=','> <if test='username != null and username != \"\"'>#{username},</if> <if test='passward != null and passward != \"\"'>#{passward},</if> <if test='phone != null and phone != \"\"'>#{phone},</if> <if test='email != null and email != \"\"'>#{email},</if> <if test='nickname != null and nickname != \"\"'>#{nickname},</if> </trim> </script>")
    public int insertUser(Map<String, Object> userMap);

    @Delete("<script> delete * from user where id=#{id}</script>")
    public int deleteUser(Integer id);

    @Update("<script> update user <trim prefix='set' suffixOverrides=','> <if test='username!=null'> username=#{username}, </if> <if test='passward!=null'> passward=#{passward}, </if> <if test='phone!=null'> phone=#{phone}, </if> <if test='email!=null'> email=#{email}, </if> <if test='dr!=null'> dr=#{dr}, </if> <if test='nickname!=null'> nickname=#{nickname}, </if></trim> where id=#{id}</script>")
    public int updateUser(Map<String,Object> userMap);

    @Select("<script> select * from user where 1=1 <if test='id!=null'>and id=#{id} </if> <if test='username!=null'>and username=#{username} </if> <if test='passward!=null'>and passward=#{passward} </if> <if test='phone!=null'>and phone=#{phone} </if> <if test='email!=null'>and email=#{email} </if> <if test='nickname!=null'>and nickname like CONCAT('%',#{nickname},'%') </if></script>")
    public List<Map<String,Object>> selectUser(Map<String,Object> userMap);

    @Select("<script> select * from user </script>")
    public List<Map<String,Object>> selectUsers();
}
